﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
public partial class Admin_UserControl_HocVien_viewLevel : System.Web.UI.UserControl
{
#region "Member variables"
    private int intUserID = 0;
    private int intHocVienID = 0;
    private int intModuleID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DAHocVien objHocVien;
    DANguoidung objNguoidung;
    DANguoiTimNguon objNguoiTimNguon;
    DATrangThaiGoi objTrangThaiGoi;
    DANguon objNguon;
    DAHocVienLV2 objLV2;
    DAHocVienLV3LV5 objLV3LV5;
    DAHocVienLV9 objLV9;
    DAHocVienLV6LV8 objLV6LV8;
    DATrangThai objTrangThai;
    DATruong objTruong;
    int intLevelID = 1;
#endregion
#region "Private methods"
    private void loadTitle(string strTen, string strMaHocVien)
    {
        try
        {
            System.Web.UI.HtmlControls.HtmlControl t = (System.Web.UI.HtmlControls.HtmlControl)this.Page.FindControl("lblTitle");
            t.Controls.Clear();
            t.Controls.Add(new LiteralControl(strMaHocVien + " - " + strTen));
        }
        catch { }
    }
    // Hien thi NguoiTimNguon tren datagrid
    private void loadHocVienID(int intHocVienID)
    {
        DataRow drHocVien = objHocVien.loadHocVienID(intHocVienID);
        if (drHocVien != null)
        {
            this.hdfHocVienID.Value = intHocVienID.ToString();
            BindData.bindTextBox(this.txtDiaChiThu, "", drHocVien["DiaChiThu"].ToString());
            BindData.bindTextBox(this.txtDienThoai, "", drHocVien["DienThoai"].ToString());
            BindData.bindTextBox(this.txtDiLam, "", drHocVien["DiLam"].ToString());
            BindData.bindTextBox(this.txtEmail, "", drHocVien["Email"].ToString());
            BindData.bindTextBox(this.txtHoTen, "", drHocVien["HoTen"].ToString());
            BindData.bindTextBox(this.txtMaHocVien, "", drHocVien["MaHocVien"].ToString());
            BindData.bindTextBox(this.txtGhiChuNguon, "", drHocVien["GhiChuNguon"].ToString());
             if (drHocVien["NgaySinh"].GetType() != typeof(DBNull))
            {
                DateTime dateStartDate = Convert.ToDateTime(drHocVien["NgaySinh"].ToString().Trim());
                this.txtNgaySinh.SelectedDate = dateStartDate;
            }
            bool strSex = Convert.ToBoolean(drHocVien["GioiTinh"].ToString());
            if (strSex)
            {
                this.ddlGioiTinh.Items[0].Selected = true;
            }
            else
            {
                this.ddlGioiTinh.Items[1].Selected = true;
            }
            int intNguonID = Convert.ToInt32(drHocVien["NguonID"].ToString());
            if (intNguonID > 0)
            {
                BindData.selectDropDowList(this.ddlNguon, drHocVien["NguonID"].ToString());
                loadNguoiTimNguon();
                BindData.selectDropDowList(this.ddlNguoiTimNguon, drHocVien["NguoiTimNguonID"].ToString());
            }
            int intTruongID = Convert.ToInt32(drHocVien["TruongID"].ToString());
            if (intTruongID > 0)
            {
                BindData.selectDropDowList(this.ddlTruong, drHocVien["TruongID"].ToString());
                loadNguoiTimNguon();
                BindData.selectDropDowList(this.ddlNguoiTimNguon, drHocVien["NguoiTimNguonID"].ToString());
            }       
            loadTitle(drHocVien["HoTen"].ToString(), drHocVien["MaHocVien"].ToString());
        }
    }
    private void loadNguon()
    {
        DataSet dsNguon = objNguon.LayDanhSachNguon();
        BindData.bindDropDownList(this.ddlNguon, dsNguon, "ID", "Ten");
    }
    private void loadTruong()
    {
        DataSet dsTruong = objTruong.LayDanhSachTruong();
        BindData.bindDropDownList(this.ddlTruong, dsTruong, "ID", "Ten");
    }
    private void loadNguoiTimNguon()
    {
        DataSet dsNguon = objNguoiTimNguon.loadNguoiTimNguon();
        BindData.bindDropDownList(this.ddlNguoiTimNguon, dsNguon, "ID", "HoTen");
    }
    private void ResetHocVien()
    {
        this.txtMaHocVien.Text = "";
        this.txtHoTen.Text = "";
        this.txtDienThoai.Text = "";
        this.txtEmail.Text = "";
        this.txtNgaySinh.Text = "";
        this.txtDiaChiThu.Text = "";
        this.txtDiLam.Text = "";
    }
    //LV2
    private void loadLV2ID(int LevelID)
    {
        DataRow drLV2 = objLV2.loadHocVienLV2ID(intHocVienID, LevelID);
        BindData.bindTextBox(this.txtCD, "", drLV2["CaoDang"].ToString());
        BindData.bindTextBox(this.txtDH, "", drLV2["DaiHoc"].ToString());
        BindData.bindTextBox(this.txtPTTH, "", drLV2["PTTH"].ToString());
        BindData.bindTextBox(this.txtTrungCap, "", drLV2["TrungCap"].ToString());
        BindData.bindTextBox(this.txtKhactotnghiepLV2, "", drLV2["KhacTotNghiep"].ToString());
        BindData.bindTextBox(this.txtkhaclaybangLV2, "", drLV2["KhacLayBang"].ToString());
        BindData.bindTextBox(this.txtghichukhacLV2, "", drLV2["GhiChu"].ToString());
        DateTime dateStartDate = Convert.ToDateTime(drLV2["NgayGoi"].ToString().Trim());
        this.txtNgayGoiLV2.SelectedDate = dateStartDate;
        BindData.bindTextBox(this.txtNoiDungNgayGoiLV2, "", drLV2["NoiDungNgayGoi"].ToString());       
        if (!drLV2["CaoDang"].ToString().Equals(""))
        {
            this.cbxCD.Checked = true;
            this.txtCD.Enabled = true;
        }
        if (!drLV2["DaiHoc"].ToString().Equals(""))
        {
            this.cbxDH.Checked = true;
            this.txtDH.Enabled = true;
        }
        if (!drLV2["PTTH"].ToString().Equals(""))
        {
            this.cbxPTTH.Checked = true;
            this.txtPTTH.Enabled = true;
        }
        if (!drLV2["TrungCap"].ToString().Equals(""))
        {
            this.cbxTrungCap.Checked = true;
            this.txtTrungCap.Enabled = true;
        }
        if (!drLV2["KhacTotNghiep"].ToString().Equals(""))
        {
            this.cbxKhactotnghiep.Checked = true;
            this.txtKhactotnghiepLV2.Enabled = true;
        }
        if (!this.txtghichukhacLV2.Text.Trim().Equals(""))
        {
            this.cbxkhaclaybangLV2.Checked = true;
            this.txtghichukhacLV2.Enabled = true;
        }
        this.cbxquantri.Checked = Convert.ToBoolean(drLV2["QuanTriKinhDoanh"].ToString().Trim());
        this.cbxKetoan.Checked = Convert.ToBoolean(drLV2["KeToan"].ToString().Trim());
        this.cbxTinhoc.Checked = Convert.ToBoolean(drLV2["TinHocUngDung"].ToString().Trim());
        this.cbxtaichinh.Checked = Convert.ToBoolean(drLV2["TaiChinhNganHang"].ToString().Trim());
        if (this.cbxquantri.Checked == true || this.cbxKetoan.Checked == true ||
            this.cbxTinhoc.Checked == true || this.cbxtaichinh.Checked == true || !this.txtkhaclaybangLV2.Text.Trim().Equals(""))
        {
            this.rdbCoLV2.Checked = true;
            this.rdbKhongLV2.Checked = false;
            this.cbxquantri.Attributes.Remove("disabled");
            this.cbxKetoan.Attributes.Remove("disabled");
            this.cbxTinhoc.Attributes.Remove("disabled");
            this.cbxtaichinh.Attributes.Remove("disabled");
            this.cbxkhaclaybangLV2.Attributes.Remove("disabled");
            this.txtghichukhacLV2.Enabled = true;
            if (this.txtkhaclaybangLV2.Text.Trim().Equals(""))
            {
                this.txtkhaclaybangLV2.Enabled = false;
                this.cbxkhaclaybangLV2.Checked = false;
            }
            else
            {
                this.txtkhaclaybangLV2.Enabled = true;
                this.cbxkhaclaybangLV2.Checked = true;
            }
        }
        else
        {
            if (this.txtkhaclaybangLV2.Text.Trim().Equals(""))
            {
                this.txtkhaclaybangLV2.Enabled = false;
                this.cbxkhaclaybangLV2.Checked = false;
            }
            else
            {
                this.cbxkhaclaybangLV2.Checked = true;
            }
            this.rdbCoLV2.Checked = false;
            this.rdbKhongLV2.Checked = true;
        }

    }

    //LV3-LV5B
    private void loadLV3LV5BID(int LevelID)
    {
        DataRow drLV3LV5 = objLV3LV5.loadHocVienLV3LV5ID(intHocVienID, LevelID);
        BindData.bindTextBox(this.txtlichhenLV3LV5, "", drLV3LV5["LichHen"].ToString());
        BindData.bindTextBox(this.txtKhongLichHenLV3LV5, "", drLV3LV5["LyDoTuChoi"].ToString());
        BindData.bindTextBox(this.txtGoilaiLV3LV5, "", drLV3LV5["GoiLai"].ToString());
        BindData.bindTextBox(this.txtgoilailan1LV3LV5, "", drLV3LV5["GoiLan1"].ToString());
        BindData.bindTextBox(this.txtgoilan2LV3LV5, "", drLV3LV5["GoiLan2"].ToString());
        BindData.bindTextBox(this.txtgoilan3LV3LV5, "", drLV3LV5["GoiLan3"].ToString());
        BindData.bindTextBox(this.txtghichu1LV3LV5, "", drLV3LV5["Ghichu1"].ToString());
        BindData.bindTextBox(this.txtghichu2LV3LV5, "", drLV3LV5["Ghichu2"].ToString());
        BindData.bindTextBox(this.txtghichuLV3LV5, "", drLV3LV5["GhiChuKhac"].ToString());
        BindData.bindTextBox(this.txtlydochuagapLV3LV5, "", drLV3LV5["LydoChuaGap"].ToString());
        BindData.bindTextBox(this.txtdangkydutuyenLV3LV5, "", drLV3LV5["DangKyDuTuyen"].ToString());
        BindData.bindTextBox(this.txtnengoilaigapLV3LV5, "", drLV3LV5["NenGoiLaiGap"].ToString());
        BindData.bindTextBox(this.txtchuanopphieuLV3LV5, "", drLV3LV5["NopPhieu"].ToString());
        BindData.bindTextBox(this.txtnengoilaidangkyLV3LV5, "", drLV3LV5["NenGoiLaiDangKy"].ToString());
        DateTime dateStartDate = Convert.ToDateTime(drLV3LV5["NgayGoi"].ToString().Trim());
        this.txtNgayGoiGanNhatLV3LV5.SelectedDate = dateStartDate;
        BindData.bindTextBox(this.txtNoiDungNgayGoiGanNhatLV3LV5, "", drLV3LV5["NoiDungNgayGoi"].ToString());
        if (drLV3LV5["Ghichu3"].ToString().ToUpper().Equals("TRUE"))
        {
            this.rdblghichu3LV3Lv5.Items[0].Selected = true;
            this.rdblghichu3LV3Lv5.Items[1].Selected = false;
        }
        else
        {
            this.rdblghichu3LV3Lv5.Items[0].Selected = false;
            this.rdblghichu3LV3Lv5.Items[1].Selected = true;
        }
        if (!drLV3LV5["LichHen"].ToString().Equals(""))
        {
            this.rdbLichhenLV3LV5.Checked = true;
            this.txtlichhenLV3LV5.Enabled = true;
        }
        else
        {
            this.rdbLichhenLV3LV5.Checked = false;
            this.txtlichhenLV3LV5.Enabled = false;
        }
        if (!drLV3LV5["LyDoTuChoi"].ToString().Equals(""))
        {
            this.rdbKhongLichhenLV3LV5.Checked = true;
            this.txtKhongLichHenLV3LV5.Enabled = true;
        }
        else
        {
            this.rdbKhongLichhenLV3LV5.Checked = false;
            this.txtKhongLichHenLV3LV5.Enabled = false;
        }
        if (!drLV3LV5["LydoChuaGap"].ToString().Equals(""))
        {
            this.rblRoigaptructiepLV3LV5.Checked = false;
            this.rblChuatoigaptructiepLV3LV5.Checked = true;
            this.txtlydochuagapLV3LV5.Enabled = true;
        }
        else
        {
            this.rblRoigaptructiepLV3LV5.Checked = true;
            this.rblChuatoigaptructiepLV3LV5.Checked = false;
            this.txtlydochuagapLV3LV5.Enabled = false;
        }
        if (!drLV3LV5["DangKyDuTuyen"].ToString().Equals(""))
        {
            this.rdblCodangkydutuyenLV3LV5.Checked = false;
            this.rdblKhongdangkydutuyenLV3LV5.Checked = true;
            this.txtdangkydutuyenLV3LV5.Enabled = true;
        }
        else
        {
            this.rdblCodangkydutuyenLV3LV5.Checked = true;
            this.rdblKhongdangkydutuyenLV3LV5.Checked = false;
            this.txtdangkydutuyenLV3LV5.Enabled = false;
        }

        if (!drLV3LV5["NopPhieu"].ToString().Equals(""))
        {
            this.rdblRoiNopPhieuLV3LV5.Checked = false;
            this.rdblChuaNopPhieuLV3LV5.Checked = true;
            this.txtchuanopphieuLV3LV5.Enabled = true;
        }
        else
        {
            this.rdblRoiNopPhieuLV3LV5.Checked = true;
            this.rdblChuaNopPhieuLV3LV5.Checked = false;
            this.txtchuanopphieuLV3LV5.Enabled = false;
        }

    }
    //LV6-LV8
    private void loadL6L8ID(int LevelID)
    {
        DataRow drLV6LV8 = objLV6LV8.loadHocVienLV6LV8ID(intHocVienID, LevelID);
        BindData.bindTextBox(this.txtdenthiLV6LV8, "", drLV6LV8["DenThi"].ToString());
        BindData.bindTextBox(this.txtBaithiLV6LV8, "", drLV6LV8["BaiThi"].ToString());
        BindData.bindTextBox(this.txtcodiemLV6LV8, "", drLV6LV8["CoDiem"].ToString());
        BindData.bindTextBox(this.txtgiaybaotrungtuyenLV6LV8, "", drLV6LV8["GiayBaoTrungTuyen"].ToString());
        BindData.bindTextBox(this.txtnhandcgiaybaotrungtuyenLV6LV8, "", drLV6LV8["NhanDuocGiayBaoTrungTuyen"].ToString());
        BindData.bindTextBox(this.txtnophocphiLV6LV8, "", drLV6LV8["NopHocPhi"].ToString());
        BindData.bindTextBox(this.txtNopHoSoLV6LV8, "", drLV6LV8["NopHoSo"].ToString()); ;
        BindData.bindTextBox(this.txtNgayGoiGanNhatV6LV8, "", drLV6LV8["NgayGoi"].ToString());
        BindData.bindTextBox(this.txtNoiDungNgayGoiGanNhatV6LV8, "", drLV6LV8["NoiDungNgayGoi"].ToString());
        BindData.bindTextBox(this.txtDuKienNhapHoc, "", drLV6LV8["DuKienNhapHoc"].ToString());
        BindData.bindTextBox(this.txtThucNhapHoc, "", drLV6LV8["ThucNhapHoc"].ToString());
    }
#endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
#endregion
#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
       
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objHocVien = new DAHocVien(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
        objNguon = new DANguon(sqlConnec);
        objTruong = new DATruong(sqlConnec);
        objNguoiTimNguon = new DANguoiTimNguon(sqlConnec);
        objTrangThaiGoi = new DATrangThaiGoi(sqlConnec);
        objLV2 = new DAHocVienLV2(sqlConnec);
        objLV3LV5 = new DAHocVienLV3LV5(sqlConnec);
        objLV6LV8 = new DAHocVienLV6LV8(sqlConnec);
        objTrangThai = new DATrangThai(sqlConnec);
        objLV9 = new DAHocVienLV9(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        loadDataAccess();
        if (Request.QueryString["HocVienID"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["HocVienID"].Trim());
            if (boolCheck)
            {
                intHocVienID = Convert.ToInt32(Request.QueryString["HocVienID"].Trim());
                loadHocVienID(intHocVienID);
            }
        } if (Request.QueryString["LevelView"] != null)
        {
            bool boolCheck = StringUntil.checkNumber(Request.QueryString["LevelView"].Trim());
            if (boolCheck)
            {
                intLevelID = Convert.ToInt32(Request.QueryString["LevelView"].Trim());
                if (intLevelID == 1)
                {
                    try
                    {
                        loadLV2ID(2);
                    }
                    catch { }
                    this.pnL2.Visible = true;
                    this.pnL3_5B.Enabled = false;
                    this.pnL6_8.Enabled = false;
                }
                else if (intLevelID > 1 && intLevelID <= 5)
                {
                    loadLV2ID(2);
                    this.pnL2.Enabled = false;
                    this.pnL3_5B.Enabled = true;
                    this.pnL6_8.Enabled = false;
                }
                else if (intLevelID >= 6 && intLevelID <= 8)
                {
                    loadLV2ID(2);
                    this.pnL2.Enabled = false;
                    this.pnL3_5B.Enabled = false;
                    this.pnL6_8.Enabled = true;
                }
                else if (intLevelID == 9)
                {
                    loadLV2ID(2);
                    this.pnL2.Enabled = false;
                    this.pnL3_5B.Enabled = false;
                    this.pnL6_8.Enabled = false;
                }
                if (intLevelID == 2 || intLevelID == 3)
                {
                    try
                    {
                        loadLV3LV5BID(3);
                    }
                    catch { }
                }
                else if (intLevelID > 3 && intLevelID < 6)
                {
                    loadLV3LV5BID(intLevelID);
                    try
                    {
                        loadLV3LV5BID(intLevelID + 1);
                    }
                    catch { }

                }
                else if (intLevelID == 6)
                {
                    loadLV3LV5BID(6);
                    try
                    {
                        loadL6L8ID(7);
                    }
                    catch { }
                }
                else if (intLevelID > 6 && intLevelID < 9)
                {
                    loadLV3LV5BID(6);
                    loadL6L8ID(intLevelID);
                    try
                    {
                        loadL6L8ID(intLevelID + 1);
                    }
                    catch { }
                }
                else if (intLevelID == 9)
                {
                    loadLV3LV5BID(6);
                    loadL6L8ID(9);
                }
                else if (intLevelID == 10)
                {
                    loadLV3LV5BID(6);
                    loadL6L8ID(9);
                    loadLV9ID(10, true);
                }

            }
        }
        if (!Page.IsPostBack)
        {
            CheckUserModule(intUserID, intModuleID);
            loadNguon();
            loadNguoiTimNguon();
            loadTruong();
        }
        this.hplCloseWindows.Attributes.Add("OnClick", "javascript:window.opener='x';window.close();");
        this.btnClose.Attributes.Add("OnClick", "javascript:window.opener='x';window.close();");
        this.pnL2.Enabled = false;
        this.pnL3_5B.Enabled = false;
        this.pnL6_8.Enabled = false;
        this.pnL10.Enabled = false;
    }
    private void loadLV9ID(int LevelID, bool boolCheck)
    {
        try
        {
            DataRow drLV9 = objLV9.loadHocVienLV9ID(intHocVienID, LevelID);

            this.cbxNophocphiL9.Checked = Convert.ToBoolean(drLV9["Nophocphi"].ToString().Trim());
            this.cbxNophosoL9.Checked = Convert.ToBoolean(drLV9["Nophoso"].ToString().Trim());
            DateTime dateNgaynophocphi = Convert.ToDateTime(drLV9["Ngaynophocphi"].ToString().Trim());
            this.txtNgaynophocphiL9.SelectedDate = dateNgaynophocphi;
            DateTime dateNgaynophoso = Convert.ToDateTime(drLV9["Ngaynophoso"].ToString().Trim());
            this.txtNgayHoanThanhL9.SelectedDate = dateNgaynophoso;
            DateTime dateNgayHoanThanh = Convert.ToDateTime(drLV9["NgayHoanThanh"].ToString().Trim());
            this.txtNgayHoanThanhL9.SelectedDate = dateNgayHoanThanh;
            BindData.bindTextBox(this.txtNoiDungHoanThanhL9, "", drLV9["NoiDungHoanThanh"].ToString());
        }
        catch { }
    }
#endregion
}
